package cn.qhuan.modules.service.impl;

import cn.qhuan.modules.constos.PageResponseResult;
import cn.qhuan.modules.constos.StatusConstos;
import cn.qhuan.modules.dtos.NewsAuthDto;
import cn.qhuan.modules.dtos.NewsDto;
import cn.qhuan.modules.entity.News;
import cn.qhuan.modules.mapper.NewsMapper;
import cn.qhuan.modules.service.INewsService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 新闻动态表
 * @Author: 司翔宇
 * @Date:   2020-12-30
 * @Version: V1.0
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService {
    @Autowired
    private NewsMapper newsMapper;
    @Override
    public Result<?> loadNewsByPushTime(NewsDto newsDto) {
        //1.检查参数
        if (newsDto == null || newsDto.getArtistId() == null){
            return Result.error("缺少参数");
        }
        //进行分页条件查询//进行分页条件查询
        newsDto.checkParam();
        IPage pageParam =new Page(newsDto.getPage(),newsDto.getSize());
        LambdaQueryWrapper<News> lambdaQueryWrapper =new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(newsDto.getArtistId())){
            lambdaQueryWrapper.eq(News::getArtistId,newsDto.getArtistId())
                    .eq(News::getState,StatusConstos.STATUS_PASS)
                    .orderByDesc(News::getPushTime);
        }

        //3.封装结果返回
        IPage pageList = page(pageParam, lambdaQueryWrapper);
        PageResponseResult responseResult =new PageResponseResult(newsDto.getPage(),newsDto.getSize(), (int) pageList.getTotal());
        responseResult.setResult(pageList.getRecords());
        return responseResult;
    }

    @Override
    public Result<?> updateStatus(NewsAuthDto dto, Integer type) {
        //1.检查参数
        if (dto ==null || dto.getId() == null ){
            return Result.error("缺少参数");
        }
        //2.查询媒体报道信息
        News news = newsMapper.selectById(dto.getId());

        if (news == null){
            return Result.error("媒体报道不存在");
        }
        //3.审核失败
        if (type.equals(StatusConstos.STATUS_FAIL)){
            updateArtist(news,StatusConstos.STATUS_FAIL);
        }else if (type.equals(StatusConstos.STATUS_PASS)){
            updateArtist(news,StatusConstos.STATUS_PASS);
        }
        return Result.OK("修改成功");
    }
    /**
     * 修改作品信息
     * @param news
     * @param status
     *
     */
    private void updateArtist (News news,Integer status){
        news.setState(status);
        newsMapper.updateById(news);
    }
}
